﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.Linq.Expressions;
using System.Web.Mvc.Html;
using Storytelling.Entidades;
using Storytelling.Entidades.VTR;
using Storytelling.Models;

namespace Storytelling.Utils
{
    public static class View
    {
        public static MvcHtmlString Poder(this HtmlHelper htmlHelper, Acao acao)
        {
            string nome = "";

            if (acao is AcaoDisciplina)
            {
                nome = string.Format("{0} (Disciplina)", ((AcaoDisciplina)acao).Disciplina.Nome);
            }

            return new MvcHtmlString(nome);
        }

        public static MvcHtmlString Evolucao(this HtmlHelper htmlHelper, TipoCampo tipoCampo, Campo campo, int peso)
        {
            return new MvcHtmlString(string.Format("id=\"{0}_{1}_{2}\"", (int)tipoCampo, peso, (int)campo));
        }

        public static MvcHtmlString EvolucaoPotenciaSangue(this HtmlHelper htmlHelper)
        {
            return new MvcHtmlString(string.Format("id=\"{0}_8_0\"", (int)TipoCampo.Extensor));
        }

        public static MvcHtmlString Pontos(this HtmlHelper htmlHelper, int quant, int total = 0)
        {
            StringBuilder stringBuilder = new StringBuilder();

            if (total == 0)
            {
                total = quant;
            }

            for (int i = 1; i <= total; i++)
            {
                if (i <= quant)
                {
                    stringBuilder.Append("\u25CF");                
                }
                else
                {
                    stringBuilder.Append("\u25CC");                 
             
                }
            }

            return new MvcHtmlString(stringBuilder.ToString());
        }

        public static MvcHtmlString Caixas(this HtmlHelper htmlHelper, int quant, int total = 0)
        {
            StringBuilder stringBuilder = new StringBuilder();

            if (total == 0)
            {
                total = quant;
            }

            for (int i = 1; i <= total; i++)
            {
                if (i <= quant)
                {
                    stringBuilder.Append("\u25A0");
                }
                else
                {
                    stringBuilder.Append("\u25A1");

                }
            }

            return new MvcHtmlString(stringBuilder.ToString());
        }

        public static MvcHtmlString LinhasDinamicas(this HtmlHelper htmlHelper, Func<int, object> html, params int[] quantColecoes)
        {
            StringBuilder stringBuilder = new StringBuilder();

            for (int i = 0; i < quantColecoes.Max(); i++)
            {
                TagBuilder tagTr = new TagBuilder("tr");
                tagTr.InnerHtml = html(i).ToString();
                stringBuilder.Append(tagTr.ToString());
            }

            return new MvcHtmlString(stringBuilder.ToString());
        }

        public static TReturn TentarPassar<TModel, TReturn>(this HtmlHelper<TModel> htmlHelper, int index, TReturn padrao, Func<TModel, TReturn> value) 
        {
            var obj = htmlHelper.ViewDataContainer.ViewData.Model;
            return index >= 0 && obj != null ? value((TModel)obj) : padrao;
        }

        public static MvcHtmlString Filtro(this HtmlHelper htmlHelper, string id, Func<object, object> html)
        {
            TagBuilder tagFieldset = new TagBuilder("fieldset");

            TagBuilder tagLegend = new TagBuilder("legend");

            TagBuilder tagDiv = new TagBuilder("div");

            TagBuilder tagLink = new TagBuilder("a");

            tagLink.MergeAttribute("style", "cursor:pointer");
            tagLink.MergeAttribute("onclick", string.Format("exibir(this,'{0}','\u25B2','\u25BC')", id));
            tagLink.SetInnerText("\u25BC");

            tagLegend.InnerHtml = string.Format("{0} Filtro", tagLink.ToString(TagRenderMode.Normal));

            TagBuilder tagConteudo = new TagBuilder("div");

            tagConteudo.GenerateId(id);
            tagConteudo.MergeAttribute("style", "display:none");
            tagConteudo.InnerHtml = html(null).ToString();

            tagDiv.InnerHtml = tagConteudo.ToString(TagRenderMode.Normal);
            tagFieldset.InnerHtml = string.Format("{0}{1}", tagLegend.ToString(TagRenderMode.Normal), tagDiv.ToString(TagRenderMode.Normal));

            return new MvcHtmlString(tagFieldset.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString Opcoes(this HtmlHelper htmlHelper, bool salvar, string controlador, params KeyValuePair<string, string>[] botoes)
        {
            OpcoesViewModel opcoesViewModel = new OpcoesViewModel();

            opcoesViewModel.Salvar = salvar;
            opcoesViewModel.Controlador = controlador;
            opcoesViewModel.Botoes = botoes;

            return htmlHelper.Partial("_OpcoesPartial", opcoesViewModel);
        }

        public static MvcHtmlString DivOculta<T>(this HtmlHelper htmlHelper, string id, T item, string rotuloEsconder, string rotuloAparecer, Func<T,object> html)
        {
            TagBuilder tagDivPadrao = new TagBuilder("div");

            TagBuilder tagLink = new TagBuilder("a");

            tagLink.MergeAttribute("style", "cursor:pointer");
            tagLink.MergeAttribute("onclick", string.Format("exibir(this,'{0}','{1}','{2}')", id, rotuloEsconder, rotuloAparecer));
            tagLink.SetInnerText(rotuloAparecer);

            tagDivPadrao.InnerHtml = tagLink.ToString(TagRenderMode.Normal);

            TagBuilder tagConteudo = new TagBuilder("div");

            tagConteudo.GenerateId(id);
            tagConteudo.MergeAttribute("style", "display:none");
            tagConteudo.InnerHtml = html(item).ToString();

            tagDivPadrao.InnerHtml += tagConteudo.ToString(TagRenderMode.Normal);

            return new MvcHtmlString(tagDivPadrao.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString DescricaoOculta(this HtmlHelper htmlHelper, string id, int colunas, string rotuloEsconder, string rotuloAparecer, string descricao)
        {
            TagBuilder tagDivPadrao = new TagBuilder("div");

            TagBuilder tagLink = new TagBuilder("a");

            tagLink.MergeAttribute("style", "cursor:pointer");
            tagLink.MergeAttribute("onclick", string.Format("exibir(this,'{0}','{1}','{2}')", id, rotuloEsconder, rotuloAparecer));
            tagLink.SetInnerText(rotuloAparecer);

            tagDivPadrao.InnerHtml = tagLink.ToString(TagRenderMode.Normal);

            TagBuilder tagTextArea = new TagBuilder("textarea");

            tagTextArea.GenerateId(id);
            tagTextArea.MergeAttribute("style", "display:none");
            tagTextArea.MergeAttribute("rows", "1");
            tagTextArea.MergeAttribute("readonly", "readonly");
            tagTextArea.MergeAttribute("cols", colunas.ToString());
            tagTextArea.SetInnerText(descricao);

            tagDivPadrao.InnerHtml += tagTextArea.ToString(TagRenderMode.Normal);

            return new MvcHtmlString(tagDivPadrao.ToString(TagRenderMode.Normal));
        }

        public static MvcHtmlString Clone(this HtmlHelper htmlHelper, string id, string rotulo, Type tipoEntidade, long[] idsEntidades, Func<int, object> html)
        {
            TagBuilder tagDivPadrao = new TagBuilder("div");

            for (int i = 0; i < idsEntidades.Length; i++)
            {
                TagBuilder tagDiv = new TagBuilder("div");
                tagDiv.GenerateId(string.Format("{0}_{1}", id, i));

                TagBuilder tagFieldset = new TagBuilder("fieldset");
                TagBuilder tagLegend = new TagBuilder("legend");

                tagLegend.MergeAttribute("align", "right");

                TagBuilder tagBotaoRemover = new TagBuilder("input");
                tagBotaoRemover.MergeAttribute("type", "button");
                tagBotaoRemover.MergeAttribute("value", "X");
                tagBotaoRemover.MergeAttribute("onclick", string.Format("excluirRemoverClone('{0}',{1},'{2}',{3})", id, i, tipoEntidade.Name, idsEntidades[i]));

                tagLegend.InnerHtml = tagBotaoRemover.ToString(TagRenderMode.SelfClosing);
                tagFieldset.InnerHtml = tagLegend.ToString(TagRenderMode.Normal);

                tagFieldset.InnerHtml += html(i).ToString();

                tagDiv.InnerHtml = tagFieldset.ToString(TagRenderMode.Normal);
                tagDivPadrao.InnerHtml += tagDiv.ToString(TagRenderMode.Normal);
            }

            TagBuilder tagDivOriginal = new TagBuilder("div");
            tagDivOriginal.GenerateId(id);
            tagDivOriginal.InnerHtml = html(-1).ToString();

            TagBuilder tagDivHidden = new TagBuilder("div");
            tagDivHidden.GenerateId(string.Format("cloneHidden_{0}", id));
            tagDivHidden.MergeAttribute("style", "display: none");

            TagBuilder tagContador = new TagBuilder("input");
            tagContador.GenerateId(string.Format("contador_{0}", id));
            tagContador.MergeAttribute("type", "hidden");
            tagContador.MergeAttribute("value", idsEntidades.Length.ToString());
            tagDivHidden.InnerHtml = tagContador.ToString(TagRenderMode.SelfClosing);

            tagDivHidden.InnerHtml += tagDivOriginal.ToString(TagRenderMode.Normal);
            tagDivPadrao.InnerHtml += tagDivHidden.ToString(TagRenderMode.Normal);

            TagBuilder tagDivCentro = new TagBuilder("div");
            tagDivCentro.MergeAttribute("style", "text-align:center");

            TagBuilder tagBotaoMais = new TagBuilder("input");
            tagBotaoMais.MergeAttribute("type", "button");
            tagBotaoMais.MergeAttribute("value", rotulo);
            tagBotaoMais.MergeAttribute("onclick", string.Format("clonar('{0}')", id));

            tagDivCentro.InnerHtml = tagBotaoMais.ToString(TagRenderMode.SelfClosing);
            tagDivPadrao.InnerHtml += tagDivCentro.ToString(TagRenderMode.Normal);

            return new MvcHtmlString(tagDivPadrao.ToString(TagRenderMode.Normal));
        }
    }
}